package com.flashflexpro.flexdirector.adapter.codec {
import com.flashflexpro.flexdirector.adapter.descriptor.DescPropertyAuto;
import com.flashflexpro.flexdirector.model.CodeStringValue;

import flash.events.KeyboardEvent;
import flash.events.MouseEvent;

import mx.automation.IAutomationObject;

public class CodecTriggerEvent extends CodecDefault{


    override public function encode(obj:Object, descProperty:DescPropertyAuto, relativeParent:IAutomationObject ):CodeStringValue{
        var val:Object = getMemberFromObject( obj, descProperty );
        var str:String;
        if( val is MouseEvent){
            str = "mouseEvent";
        }
        if( val is KeyboardEvent ){
            str = "keyboardEvent";
        }
        var rt:CodeStringValue = new CodeStringValue();
        rt.desc = descProperty.getDto();
        rt.string = str;
        rt.value = str;
        rt.code = "\"" + str + "\"";
        return rt;
    }

    override public function decode(obj:Object, value:Object, descProperty:DescPropertyAuto, relativeParent:IAutomationObject, inString:Boolean = false):void {
        if( value == "mouseEvent" ){
            obj[ descProperty.name ] = new MouseEvent( MouseEvent.CLICK );
        }
        else if( value == "keyboardEvent" ){
            obj[ descProperty.name ] = new KeyboardEvent( KeyboardEvent.KEY_UP );
        }
        else{
            obj[ descProperty.name ] = null;
        }
    }
}
}